package com.ruoyi.business.controller;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.HistoricActivity;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IProcessService;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 审核列Controller
 * 
 * @author ruoyi
 * @date 2021-07-20
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController
{
    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;


    @Autowired
    private IProcessService processService;

    @RequiresPermissions("business:CarPackageAudit:view")
    @GetMapping()
    public String CarPackageAudit()
    {
        return prefix + "/CarPackageAudit";
    }

    /**
     * 查询审核列列表
     */
    @RequiresPermissions("business:CarPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核列列表
     */
    @RequiresPermissions("business:CarPackageAudit:export")
    @Log(title = "审核列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "审核列数据");
    }

    /**
     * 新增审核列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审核列
     */
    @RequiresPermissions("business:CarPackageAudit:add")
    @Log(title = "审核列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改审核列
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核列
     */
    @RequiresPermissions("business:CarPackageAudit:edit")
    @Log(title = "审核列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除审核列
     */
    @RequiresPermissions("business:CarPackageAudit:remove")
    @Log(title = "审核列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }

    @RequestMapping("/readResource")
    public void readResource(Long id, HttpServletResponse response) throws Exception {
        CarPackageAudit packageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        InputStream inputStream = null;
        if (packageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS) ||
                packageAudit.getStatus().equals(CarPackageAudit.STATUS_REJECT)){
            List<String> acticeActivitiy =  processService.getActiceActivitiyids(packageAudit.getInstanceId());
            inputStream = processService.getProcessImage(packageAudit.getProcessDefinitionId(), acticeActivitiy,Collections.EMPTY_LIST);
        }else {
            inputStream =  processService.getProcessImage(packageAudit.getProcessDefinitionId(),Collections.EMPTY_LIST,Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    /**
     * 撤销功能的实现
     * @param instanceId
     * @return
     */
    @PostMapping(value = "/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long instanceId){
        try {
            carPackageAuditService.cancelApply(instanceId);
            return AjaxResult.success("撤销成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("撤销失败");
        }
    }

    @GetMapping("/todoPage")
    public String todoPage(){
        return prefix + "/todoPage";
    }

    /**
     * 查询当前账号的待办任务,并返回到界面
     * @param carPackageAudit
     * @return
     */
    @PostMapping("/todoPagelist")
    @ResponseBody
    public TableDataInfo todoPageList(CarPackageAudit carPackageAudit){
        List<CarPackageAudit> carPackageAuditList = carPackageAuditService.todoPageList(carPackageAudit);
        return getDataTable(carPackageAuditList);
    }
    @GetMapping("/openServiceItemsEditPage")
    public String openServiceItemsEditPage(Long carPackageAuditId,ModelMap modelMap){
        CarPackageAudit audit = carPackageAuditService.selectCarPackageAuditById(carPackageAuditId);
        ServiceItem serviceItem = JSON.parseObject(audit.getServiceItemInfo(),ServiceItem.class);
        modelMap.put("serviceItem",serviceItem);
        modelMap.put("carPackageAuditId",carPackageAuditId);
        return prefix + "/serviceItemsEditPage";
    }
    /**
     * 跳转界面
     */
    @RequestMapping("/showVerifdialog")
    public String showVerifdialog(String taskId , ModelMap mmap){
        mmap.put("taskId" , taskId);
        return prefix + "/taskVerify";
    }

    @RequestMapping("/complete/{taskId}")
    @ResponseBody
    public AjaxResult complete(@PathVariable("taskId") String taskId,String auditStatus,String comment){
        carPackageAuditService.complete(taskId , auditStatus,comment);
        return success();
    }

    /**
     * 修改表单
     * @param carPackageAuditId
     * @param serviceItem
     * @return
     */
    @RequestMapping("/updateServiceItem")
    @ResponseBody
    public AjaxResult updateServiceItem(Long carPackageAuditId,ServiceItem serviceItem){
        return toAjax(carPackageAuditService.updateServiceItem(carPackageAuditId,serviceItem));
    }

    /**
     * 重新申请
     * @param taskId
     * @param carPackageAuditId
     * @return
     */
    @RequestMapping("/reApply")
    @ResponseBody
    public AjaxResult reply(String taskId,Long carPackageAuditId){
        return toAjax(carPackageAuditService.reply(taskId,carPackageAuditId));
    }

    /**
     * 我的已办
     * @return
     */
    @RequestMapping("/donePage")
    public String donePage(){
        return prefix+"/donePage";
    }

    @PostMapping("/doneList")
    @ResponseBody
    public TableDataInfo doneList(CarPackageAudit carPackageAudit){
        List<CarPackageAudit> carPackageAuditList = carPackageAuditService.donePageList(carPackageAudit);
        return getDataTable(carPackageAuditList);
    }
    @RequestMapping("/historyAudit")
    public String historyAudit(String instanceId,ModelMap modelMap){
        modelMap.put("instanceId",instanceId);
        return prefix + "/historyList";
     }

    /**
     * 历史列表
     * @return
     */
    @PostMapping("/listHistory")
    @ResponseBody
    public TableDataInfo listHistory(String instanceId){
        List<HistoricActivity> historicActivityList = carPackageAuditService.selectHistoryListByInstanceId(instanceId);
        return getDataTable(historicActivityList);
    }
}
